import React, { Component } from "react";
import "./index.css";
export default class Item extends Component {
  state = { mouse: false };
  //鼠标移动事件
  handleMouse = (flag) => {
    return () => {
      this.setState({ mouse: flag });
    };
  };
  //勾选回调
  handleCheck = (id) => {
    return (event) => {
      console.log(event.target.checked,id)
      this.props.updateTodo(id,event.target.checked)
    };
  };
  handleDelete = (id) => {
    console.log(`通知删除`, id)
    //此弹窗确定方法，不加window会报错
    if (window.confirm('确定删除吗？')) {
      this.props.deleteTodo(id)
    }
  }
  render() {
    const { todo } = this.props;
    const { mouse } = this.state;
    return (
      <li
        style={{ backgroundColor: mouse ? "#ddd" : "white" }}
        onMouseLeave={this.handleMouse(false)}
        onMouseEnter={this.handleMouse(true)}
      >
        <label>
          <input
            type="checkbox"
            checked={todo.done}
            onChange={this.handleCheck(todo.id)}
          />
          <span>{todo.name}</span>
        </label>
        <button
          className="btn btn-danger" 
          onClick={()=>this.handleDelete(todo.id)}
          style={{ display: mouse ? "block" : "none" }}
        >
          删除
        </button>
      </li>
    );
  }
}
